package com.bdqn.ch06.controller;

import com.bdqn.ch06.entity.Role;
import com.bdqn.ch06.entity.User;
import com.bdqn.ch06.service.RoleService;
import com.bdqn.ch06.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping(value = "/list")
    public String list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "usrName", required = false, defaultValue = "") String usrName,
                       @RequestParam(value = "roleId", required = false, defaultValue = "0") Long roleId,
                       Model model) {

        Map<String, String> map = new HashMap<>();
        map.put("usrName", usrName);
        map.put("roleId", roleId.toString());
        Sort sort = new Sort(Sort.Direction.ASC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
        Page<User> page = userService.findPageByMap(map, pageable);

        List<Role> roles = roleService.getRoleListService();

        model.addAttribute("userPager", page);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        model.addAttribute("roles", roles);

        return "user/list";
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Object deleteById(@RequestParam(value = "id") Long id) {
        Map<String, String> map = new HashMap<>();
        try {
            userService.deleteByIdBiz(id);
            map.put("delResult", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", "false");
        }
        return map;
    }

    @RequestMapping(value = "/add")
    public String add(Model model) {
        List<Role> roles = roleService.getRoleListService();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    @RequestMapping(value = "/save")
    public String save(User user) {
        userService.saveUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping(value = "/edit")
    public String edit(Long usrId, Model model) {
        User user = userService.getByUserId(usrId);
        List<Role> roles = roleService.getRoleListService();
        model.addAttribute("user", user);
        model.addAttribute("roles", roles);
        return "user/edit";
    }
}
